//
//  HomeViewController.swift
//  SinaWebo
//
//  Created by apple on 16/6/6.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    private struct TabBarItemName {
        static let firstName = "首页"
        static let compnent = "_highlighted"
    }
    override func viewDidLoad() {
        super.viewDidLoad()
               addControllers()
        // Do any additional setup after loading the view.
        let barButton = MainTabBar()
        setValue(barButton, forKey: "tabBar")
        barButton.addButtonItem.addTarget(self, action: "addAction", forControlEvents: .TouchUpInside)
        tabBar.tintColor = UIColor.orangeColor()  //设置被选择时的颜色渲染
    }
    
    func addAction() {
        print(__FUNCTION__)
    }
    private func addController(vc: UIViewController ,titles: String ,imageName: String) {
        //set orignal image font 
        vc.title = titles
        vc.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(.AlwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage.imageWithcompnentStr(imageName, compnent:TabBarItemName.compnent)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()],
            forState: UIControlState.Selected)
        let nav = UINavigationController(rootViewController: vc)

        addChildViewController(nav)
    }
    
    private func addControllers() {
        addController(HomeTableViewController(), titles:TabBarItemName.firstName, imageName:"tabbar_home" )
        addController(MessageTableViewController(), titles: "消息", imageName:"tabbar_message_center" )
        addController(DiscoverTableViewController(), titles: "发现", imageName:"tabbar_discover" )
        addController(ProfileTableViewController(), titles: "我", imageName:"tabbar_profile" )
    }
    }

private extension UIImage {
    static func imageWithcompnentStr(imageName: String, compnent:String) ->UIImage? {
        let name: String = imageName + compnent
        return UIImage(named: name)
    }
}